package com.meilai.project.controller.web.finance.supplier;

import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.finance.supplier.SupplierManageDTO;
import com.meilai.project.service.finance.supplier.SupplierManageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "供应商管理", tags = {"供应商管理"})
@RestController
@RequestMapping("api/web/supplier_manage")
public class SupplierManageController {

    @Autowired
    private SupplierManageService service;


    @GetMapping("list")
    @ApiOperation(value = "供应商列表")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                             @RequestParam(name = "page_size", defaultValue = "10") Integer page_size,
                             @RequestParam(name = "name", required = false) String name) {
        SupplierManageDTO searchDTO = new SupplierManageDTO();
        searchDTO.setName(name);
        return new CommonResult().success(service.selectPageList(page, page_size, searchDTO));
    }

    @GetMapping("no_page_list")
    @ApiOperation(value = "不分页供应商列表")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult noPageList(@RequestParam(name = "name", required = false) String name) {
        SupplierManageDTO searchDTO = new SupplierManageDTO();
        searchDTO.setName(name);
        return new CommonResult().success(service.selectList(searchDTO));
    }


    @ApiOperation(value = "删除", notes = "删除")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @DeleteMapping("delete")
    public CommonResult delete(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (service.delete(id)) {
                return new CommonResult().success("删除成功");
            } else {
                return new CommonResult().failed("删除失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PostMapping("add")
    @ApiOperation(value = "新增")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult add(@RequestBody SupplierManageDTO dto) {
        try {
            if (service.addOne(dto)) {
                return new CommonResult().success("新增成功");
            }
            return new CommonResult().failed("新增失败");
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    public CommonResult edit(@RequestBody SupplierManageDTO dto) {
        if (service.editOne(dto)) {
            return new CommonResult().success("修改成功");
        }
        return new CommonResult().failed("修改失败");
    }

}
